var config = require('../../config/config.js')
Page({
  data: {
    showPage:2,
    buyNumber: 0,
    onPullDownRefresh:'',
    currentId:1,
    arr: [],
    waterList: [],
    categoryList:[],
    pageUrl: config.index_goods_list,
    mid:1,
  },
  //去结算
  onTab: function (event) {
    var cartList = wx.getStorageSync('cart');
    var cartListLength = 0;
    if (!cartList) {
      cartListLength = 0;
    }
    for(var i in cartList) {
      if (cartList[i]) {
        cartListLength++;
        }
    }
    var showPage = 0;
    if (cartListLength) {
      showPage = 1;
    }
    wx.navigateTo({
      url: "cart/cart?showPage=" + showPage
    })
  },
  log: function (data) {
  },
  caculator:function(){
    var cartData = wx.getStorageSync('cart');
    var currentNum = 0;
    for (var i in cartData) {
      if (cartData[i]) {
        currentNum++;
      }
      this.setData({
        buyNumber: currentNum
      });
    }
  },


   //判断显示
  checkShow:function(e){
    var show = 2;
    if (!this.data.waterList.length){
      show = 1;
    }else{
      show = 0;
    }
    this.setData({
      showPage: show
    })
  },


  //加入购物车
  addcart: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var currentNum = this.data.buyNumber;
    var arr = that.data.arr;
    var cartData = wx.getStorageSync('cart');
    
    // 判断当前节点是否存在于购物车，如果存在则return，此次判断结束
    var flag = 0;
    for (var i in cartData) {
      if (!cartData[i]) {
        continue;
      }
      if (i == id) {
        flag = 1;
        return;
      }
    }
    //终止所有的流程
    if (flag) {
      return;
    }
    // TODO:判断当前购物车商品数量是否超过10个，如果超过十个则提示购物车最多只能添加10个商品,不超过则添加到购物车
    var currentNum = 1;
    for (var i in cartData) {
      if (cartData[i]) {
        currentNum++;
      }
    }
    // this.caculator()
    if (currentNum > 10) {
      wx.showToast({
        title: '当前商品之多只能添加10个',
        icon: "none",
        duration: 1000
      })
      return;
    }
    this.setData({
      buyNumber: currentNum
    });
    wx.showToast({
      title: '添加购物车成功',
      icon: "success",
      duration: 1000
    })
    //TODO：获取到商品列表里面的内容将他放在数组arr中,通过id去重复
    arr[id] =
      {
        'id': this.data.waterList[id].id,
        'img': this.data.waterList[id].goods_img,
        'goods_name': this.data.waterList[id].goods_name,
        'goods_price': this.data.waterList[id].goods_price,
        'goods_num': 1,
        "selected": true,
        "goods_logo": this.data.waterList[id].goods_logo,
        "goods_logoImg": this.data.waterList[id].goods_logoImg
      }

    //TODO: 把arr存储到setStorage中  cart
    this.setData({
      arr: arr
    });
    var cart = wx.setStorageSync('cart', arr);
  },

  /**
   * 页面的初始数据
   */

   // 触发bindscrolltolower事件时加载更多数据，先做判断，如果分页存在，则调用下面请求数据的方法，不存在则不调用
  onScrollLower: function (message){
    if (this.data.pageUrl) {
      this.getGoodsUrl();
    }
  },
  
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
  },
  

  //调用页面接口
  getGoodsUrl: function (event) {
    var that = this;
    var url = that.data.pageUrl;
    var id =0;
    if (event) {
      id = event.currentTarget.dataset.id;
      that.setData({
        currentId: id,
        scrollTop: 0
      })
      //将商品分类的值传给下面商品列表，通过对应的id显示对应的内容
      url = config.index_goods_list+'?category_id='+id;
    }
    //判断下一页 的地址是否存在。如果判断条件为真（下一页接口不存在），则终止整个循环
    if (!id) {
      if (!url) {
        return;
      }
    }
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: url,
      method: "GET",
      data:{
        mid: member_info.mid,
        aid: member_info.aid,
        sid: member_info.sid,
      },
      success: function (res) {

        //数组追加
        var waterList = that.data.waterList;
        var index = waterList.length;
        var goodsList = res.data.data.goods_list.data;
        if (id) {
          index = 0;
          waterList = [];
        } 
        for (var i = 0; i < goodsList.length; i++) {
          waterList[index + i] = goodsList[i];
        }
        that.setData({
          waterList: waterList,
          pageUrl: res.data.data.goods_list.next_page_url
        })
        that.checkShow()
      },
      fail: function (res) {
      }
    })
  },
  getCategoryUrl: function (event) {
    var member_info = (wx.getStorageSync('member_info'));
    var that = this;
    var categoryList = that.data.categoryList;
    wx.request({
      url: config.category_list,
      method: "GET",
      data: {
        mid: member_info.mid,
        aid: member_info.aid,
        sid: member_info.sid,
      },
      success: function (res) {
        that.setData({
          categoryList: res.data.data.goods_category_list
        })
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
    that.setData({
      categoryList: categoryList,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var member_info = (wx.getStorageSync('member_info'));
    if (!wx.getStorageSync('member_info')) {
      wx.switchTab({
        url: '../takeWater/takeWater',
      })
      return
    }
    this.getGoodsUrl();
    this.getCategoryUrl();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var cartData = wx.getStorageSync("cart");
    var currentNum = 0;
    for (var i in cartData) {
      if (cartData[i]) {
        currentNum++;
      }  
    }
    this.setData({
      buyNumber: currentNum
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})

